package edu.scau.DAO_mode.serviceimp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import javax.swing.JOptionPane;

import edu.scau.DAO_mode.daoimp.UserImp;
import edu.scau.DAO_mode.model.User;
import edu.scau.DAO_mode.service.UserService;
import edu.scau.DAO_mode.util.Error;
public class UserServiceImp implements UserService {
	private UserImp userimp=null;
    public UserServiceImp() throws Exception {
		super();
		userimp=new UserImp();
	}

	@Override
	public boolean deleteUser(User user) throws Exception {
		return userimp.delete(user);
	}

	@Override
	public boolean updateUser(User user) throws Exception {
		return userimp.update(user);
	}

	@Override
	public boolean findUser(String username) throws Exception {
		return userimp.find(username);
	}

	@Override
	public List<User> findallUser() throws Exception {
		return userimp.findall();
	}
	
	@Override
	public User returnUser(String username) throws Exception {
		return userimp.returnUser(username);
	}

	@Override
	public boolean register(User user) throws Exception {
		if (userimp.find(user.getUserName())) {
			JOptionPane.showMessageDialog(null, Error.Return(-3));
			return false;
		}
		return userimp.add(user);
	}

	@Override
	public boolean login(String username,String password) throws Exception {
		if (userimp.find(username)==false) {   //用户名不存在
			JOptionPane.showMessageDialog(null, Error.Return(-1));
			return false; 
		}
		if (userimp.returnUser(username).getPassword().equals(password)==false) { //密码错误
			JOptionPane.showMessageDialog(null, Error.Return(-2));
			return false; 
		}
		return true;//用户存在且密码正确
	}





}

